<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Choosing an API</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mysqlinfo.terminology.html">« Terminology overview</a></li>
      <li style="float: right;"><a href="mysqlinfo.library.choosing.html">Choosing a library »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="mysql.html">Overview of the MySQL PHP drivers</a></li>
    <li>Choosing an API</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mysqlinfo.api.choosing" class="chapter">
   <h1>Choosing an API</h1>

   <p class="para">
    PHP offers different APIs to connect to MySQL. Below we show
    the APIs provided by the mysqli and PDO extensions. Each code snippet
    creates a connection to a MySQL server running on &quot;example.com&quot; using
    the username &quot;user&quot; and the password &quot;password&quot;. And a query is run to
    greet the user.
   </p>
   <p class="para">
    <div class="example" id="example-1404">
     <p><strong>示例 #1 Comparing the MySQL APIs</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;mysqli<br /></span><span style="color: #0000BB">$mysqli&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;'Hello,&nbsp;dear&nbsp;MySQL&nbsp;user!'&nbsp;AS&nbsp;_message&nbsp;FROM&nbsp;DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_assoc</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'_message'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;PDO<br /></span><span style="color: #0000BB">$pdo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'mysql:host=example.com;dbname=database'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'password'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$statement&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$pdo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;'Hello,&nbsp;dear&nbsp;MySQL&nbsp;user!'&nbsp;AS&nbsp;_message&nbsp;FROM&nbsp;DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$statement</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_ASSOC</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'_message'</span><span style="color: #007700">]);</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    <em class="emphasis">Feature comparison</em>
   </p>
   <p class="para">
    The overall performance of both extensions is considered to be about
    the same. Although the performance of the extension contributes only a
    fraction of the total run time of a PHP web request.  Often, the impact is
    as low as 0.1%.
   </p>
   <table id="mysqlinfo.api.choosing.changelog" class="doctable informaltable">
    
     <thead>
      <tr>
       <th class="empty">&nbsp;</th>
       <th>ext/mysqli</th>
       <th>PDO_MySQL</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PHP version introduced</td>
       <td>5.0</td>
       <td>5.1</td>
      </tr>

      <tr>
       <td>Included with PHP 7.x and 8.x</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>Development status</td>
       <td>Active</td>
       <td>Active</td>
      </tr>

      <tr>
       <td>Lifecycle</td>
       <td>Active</td>
       <td>Active</td>
      </tr>

      <tr>
       <td>Recommended for new projects</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>OOP Interface</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>Procedural Interface</td>
       <td>Yes</td>
       <td>No</td>
      </tr>

      <tr>
       <td>API supports non-blocking, asynchronous queries with mysqlnd</td>
       <td>Yes</td>
       <td>No</td>
      </tr>

      <tr>
       <td>Persistent Connections</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>API supports Charsets</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>API supports server-side Prepared Statements</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>API supports client-side Prepared Statements</td>
       <td>No</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>API supports Stored Procedures</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>API supports Multiple Statements</td>
       <td>Yes</td>
       <td>Most</td>
      </tr>

      <tr>
       <td>API supports Transactions</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>Transactions can be controlled with SQL</td>
       <td>Yes</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>Supports all MySQL 5.1+ functionality</td>
       <td>Yes</td>
       <td>Most</td>
      </tr>

     </tbody>
    
   </table>

  </div>
</div></div></body></html>